package com.sprucetec.live.common.util;

//jdk

import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;

//httpclient

/**
* Created by meicai on 2017/7/12.
*/
public class SSLHelper {
  X509TrustManager xtm = new X509TrustManager() {
      @Override
      public void checkClientTrusted(X509Certificate[] arg0, String arg1)
              throws CertificateException {
      }
      @Override
      public void checkServerTrusted(X509Certificate[] arg0, String arg1)
              throws CertificateException {
      }
      @Override
      public X509Certificate[] getAcceptedIssuers() {
          return new X509Certificate[]{};
      }};
      public PoolingHttpClientConnectionManager GetPcm() {
          PoolingHttpClientConnectionManager pcm =null;
          try {
              //获取TLS安全协议上下文
              SSLContext context = SSLContext.getInstance("TLS");
              context.init(null, new TrustManager[]{xtm}, null);
              SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE);
              RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
                      .setExpectContinueEnabled(true)
                      .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                      .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
              Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
                      .register("http", PlainConnectionSocketFactory.INSTANCE)
                      .register("https", scsf).build();
              pcm = new PoolingHttpClientConnectionManager(sfr);
              return pcm;
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
              return  null;
          } catch (KeyManagementException e) {
              e.printStackTrace();
              return null;
          }
      }
}

